#include "StdAfx.h"
#include "ScriptParser.h"
#include "definitions.h"

namespace SNU
{
	CScriptLine::CScriptLine()
		: m_strPath(_T(""))
		, m_nBillType(0)
		, m_nBillDirection(0)
	{
	}

	CScriptLine::CScriptLine(const CString& strPath, int nBillType, int nBillDirection)
		: m_strPath(strPath)
		, m_nBillType(nBillType)
		, m_nBillDirection(nBillDirection)
	{
	}



	CScriptLine::~CScriptLine()
	{
	}

	bool CScriptParser::ParseScript(const CString& strPath, CString& strRootPath, std::vector<CScriptLine>& vecScriptlines)
	{
		CStdioFile file;
		if ( file.Open(strPath, CFile::modeRead) )
		{
			CString strBuffer;
			int nCount;
			file.ReadString(strBuffer);
			nCount = _ttoi(strBuffer);
			file.ReadString(strRootPath);
			strRootPath.Trim(_T("\""));
		
			for ( int i=0; i<nCount; i++ )
			{
				CString strBillType, strBillDirection, strLinePath;
				file.ReadString(strBuffer);

				int nPos = strBuffer.Find('\t');
				strLinePath = strBuffer.Mid(0, nPos);
				int nNextPos = strBuffer.Find('\t', nPos+1);
				strBillType = strBuffer.Mid(nPos+1, nNextPos-nPos);
				strBillDirection = strBuffer.Mid(nNextPos+1);
				strLinePath.Trim(_T("\""));
				strBillType.Trim();
				strBillDirection.Trim();
			
				CScriptLine scriptline(strRootPath+strLinePath, ParseBillType(strBillType), ParseBillDirection(strBillDirection));
				vecScriptlines.push_back(scriptline);
			}
		}
		else
		{
			return false;
		}
		return true;
	}

	int CScriptParser::ParseBillType(const CString& strBillType)
	{
		int nRet = -1;
		if ( strBillType.CompareNoCase(_T("EURO5")) == 0 ) nRet = BILLTYPE_EURO_5;
		else if ( strBillType.CompareNoCase(_T("EURO10")) == 0 ) nRet = BILLTYPE_EURO_10;
		else if ( strBillType.CompareNoCase(_T("EURO20")) == 0 ) nRet = BILLTYPE_EURO_20;
		else if ( strBillType.CompareNoCase(_T("EURO50")) == 0 ) nRet = BILLTYPE_EURO_50;
		else if ( strBillType.CompareNoCase(_T("EURO100")) == 0 ) nRet = BILLTYPE_EURO_100;
		else if ( strBillType.CompareNoCase(_T("EURO200")) == 0 ) nRet = BILLTYPE_EURO_200;
		else if ( strBillType.CompareNoCase(_T("EURO500")) == 0 ) nRet = BILLTYPE_EURO_500;
		else if ( strBillType.CompareNoCase(_T("YUAN50")) == 0 ) nRet = BILLTYPE_YUAN_50;
		return nRet;
	}

	int CScriptParser::ParseBillDirection(const CString& strBillDirection)
	{
		int nRet = -1;
		if ( strBillDirection.CompareNoCase(_T("FT")) == 0 ) nRet = BILLDIRECTION_FT;
		else if ( strBillDirection.CompareNoCase(_T("FB")) == 0 ) nRet = BILLDIRECTION_FB;
		else if ( strBillDirection.CompareNoCase(_T("BT")) == 0 ) nRet = BILLDIRECTION_BT;
		else if ( strBillDirection.CompareNoCase(_T("BB")) == 0 ) nRet = BILLDIRECTION_BB;
		return nRet;
	}
};